<?php

namespace App\Http\Controllers;

use App\Models\Integrallist;
use App\Models\Property;
use App\Models\ShopFavorites;
use App\Models\TuanFavorites;
use App\Models\TuanOrder;
use App\Models\User;
use Exception;
use Illuminate\Http\Request;

/**
 * 我的
 */
class MyController extends Controller
{
    /**
     * 我的首页
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function my_index(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            $userID = $request->get('user_id');
            if (empty($userID)) {
                throw new Exception('缺少参数');
            }
            $data['user_id'] = $userID;
            //账户余额
            $balance         = Property::where(['userID' => $userID, 'type' => 1])->value('value');
            $data['balance'] = sprintf("%.2f", $balance / 100);
            //待付款订单数
            $data['pays'] = TuanOrder::where(['user_id' => $userID, 'pay_status' => 1])->count();
            //待使用订单数
            $data['uses'] = TuanOrder::where(['user_id' => $userID, 'order_status' => 1])->count();
            //待评价订单数
            $data['comments'] = TuanOrder::where(['user_id' => $userID, 'order_status' => 2])->count();

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }

    /**
     * 我的收藏
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function my_collections(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            $userID = $request->get('user_id');
            if (empty($userID)) {
                throw new Exception('缺少参数');
            }
            $type = $request->get('type') ?? 1; //收藏类型，默认团购
            switch ($type) {
                case 1:
                    $tuans = TuanFavorites::where(['user_id' => $userID, 'closed' => 1])->paginate(10);
                    //处理数据
                    foreach ($tuans as $key => $value) {
                        $data[$key]['tuan_id']    = $value->tuan_id;
                        $data[$key]['title']      = $value->tuan_info->title;
                        $data[$key]['photo']      = $value->tuan_info->photo;
                        $data[$key]['tuan_price'] = sprintf('%.2f', $value->tuan_info->tuan_price / 100);
                        $data[$key]['industry']   = $value->tuan_info->tuan->industry->name;
                    }
                    break;
                case 2:
                    $shops = ShopFavorites::where(['user_id' => $userID, 'closed' => 1])->paginate(10);
                    //处理数据
                    foreach ($shops as $k => $val) {
                        $data[$k]['shop_id']       = $val->shop_id;
                        $data[$k]['shop_name']     = $val->shop->shop_name;
                        $data[$k]['logo']          = $val->shop->logo;
                        $data[$k]['average_price'] = sprintf('%.2f', $val->shop->average_price / 100);
                        $data[$k]['comments']      = $val->shop->user_comment->count();
                    }
                    break;
            }

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }

    /**
     * 我的积分
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function my_integrals(Request $request)
    {
        try {
            $status = 0;
            $data   = [];
            $userID = $request->get('user_id');
            $factor = $request->get('factor') ?? 0; //积分纪录条件，默认全部
            if (empty($userID)) {
                throw new Exception('缺少参数');
            }
            if (!in_array($factor, [0, 1, 2])) {
                throw new Exception("参数错误");
            }
            //我的积分
            $data['user_id']  = $userID;
            $data['integral'] = User::where(['userID' => $userID])->value('integral');
            //本月获得积分
            $beginDay     = strtotime(date('Y-m-01', strtotime(date("Y-m-d"))));
            $endDay       = strtotime(date('Y-m-d', strtotime("$beginDay +1 month -1 day")));
            $data['gets'] = Integrallist::where(['userID' => $userID, 'type' => 1])
                ->whereBetween('time', [$beginDay, $endDay])->sum('integral');
            //本月消耗积分
            $data['outs'] = Integrallist::where(['userID' => $userID, 'type' => 2])
                ->whereBetween('time', [$beginDay, $endDay])->sum('integral');

            //积分纪录
            $lists = Integrallist::where(['userID' => $userID])
                ->whereBetween('time', [$beginDay, $endDay])
                ->where(function ($query) use ($factor) {
                    if ($factor > 0) {
                        $query->where('type', $factor);
                    }
                })->get();

            //处理数据
            foreach ($lists as $key => $value) {
                $data['list'][$key]['integral'] = $value->integral;
                $data['list'][$key]['note']     = $value->note;
                $data['list'][$key]['time']     = date('Y-m-d H:i:s', $value->time);
            }

            $status = 1;
            $msg    = '查询成功';
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        return response()->json(['status' => $status, 'msg' => $msg, 'data' => $data]);
    }
}
